package com.google.gwt.http.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
import com.mycompany.mywebapp.server.SettingType;
import com.mycompany.mywebapp.shared.AllObjects;
import com.mycompany.mywebapp.shared.Sensor;
import com.mycompany.mywebapp.shared.Status;
import com.mycompany.mywebapp.shared.Variator;

public class MyRequestCallback implements RequestCallback {
	private List<Status> switcheslist = new ArrayList<Status>();
	private List<Sensor> sensorslist = new ArrayList<Sensor>();
	private List<Status> detectorslist = new ArrayList<Status>();
	private List<Variator> variatorslist = new ArrayList<Variator>();
	private AllObjects allobjects = new AllObjects(switcheslist,sensorslist,detectorslist,variatorslist);

	//List<Status> switcheslist,List<Sensor> sensorslist,List<Status> detectorslist,List<Variator> variatorslist
	
	public AllObjects GetAllObjects(){
		this.allobjects.setSwitches(this.switcheslist);
		this.allobjects.setSensors(this.sensorslist);
		this.allobjects.setVariators(this.variatorslist);
		this.allobjects.setDetectors(this.detectorslist);
		return allobjects;
	}

	@Override
	public void onResponseReceived(Request request, Response response) {

		/* Dans response.getText() on a le contenu du fichier
		 * XML recu. */

		String xml = new String();
		


		Document dom = XMLParser.parse(response.getText());
		Element racine = dom.getDocumentElement();
		XMLParser.removeWhitespace(racine);

		// Récupération des différents éléments
		Element switches = (Element)dom.getElementsByTagName("Switches").item(0);
		Element sensors = (Element)dom.getElementsByTagName("Sensors").item(0);
		Element detectors = (Element)dom.getElementsByTagName("Detectors").item(0);
		Element variators = (Element)dom.getElementsByTagName("Variators").item(0);

		XMLParser.removeWhitespace(switches);
		XMLParser.removeWhitespace(sensors);
		XMLParser.removeWhitespace(detectors);
		XMLParser.removeWhitespace(variators);

		NodeList switchesnodelist = switches.getChildNodes();
		NodeList sensorsnodelist = sensors.getChildNodes();
		NodeList detectorsnodelist = detectors.getChildNodes();
		NodeList variatorsnodelist = variators.getChildNodes();


		// On s'occupe que des switches pour cette boucle
		for (int i = 0; i < switchesnodelist.getLength(); i++) {


			Element next = (Element) switchesnodelist.item(i);
			String name = next.getTagName();
			String id = next.getAttribute("id");
			String location = next.getAttribute("location");
			Status s= new Status(name,id,location,"NULL","NULL");
			SettingType.Setting(s);
			this.switcheslist.add(s);
		}

		// On s'occupe que des sensors pour cette boucle
		for (int i = 0; i < sensorsnodelist.getLength(); i++) {


			Element next = (Element) sensorsnodelist.item(i);
			String name = next.getTagName();
			String id = next.getAttribute("id"); // les valeurs des attributs pour chaque equipement
			String location = next.getAttribute("location");

			Sensor s= new Sensor(name, id,location,"NULL","NULL");
			SettingType.Setting(s);
			this.sensorslist.add(s);						
		}

		// On s'occupe que des detectors pour cette boucle
		for (int i = 0; i < detectorsnodelist.getLength(); i++) {


			Element next = (Element) detectorsnodelist.item(i);
			String name = next.getTagName();
			String id = next.getAttribute("id"); 
			String location = next.getAttribute("location");

			Status s= new Status(name, id,location,"NULL","NULL");
			SettingType.Setting(s);
			this.detectorslist.add(s);						
		}

		// On s'occupe que des variators pour cette boucle
		for (int i = 0; i < variatorsnodelist.getLength(); i++) {


			Element next = (Element) variatorsnodelist.item(i);
			String name = next.getTagName();
			String id = next.getAttribute("id"); 
			String location = next.getAttribute("location");

			Variator s= new Variator(name, id,location,"NULL","NULL","NULL");
			SettingType.Setting(s);
			this.variatorslist.add(s);	

		}
		System.out.println("TOTO : " + this.switcheslist.get(0).getName());

	}

	@Override
	public void onError(Request request, Throwable exception) {
		Window.alert("Could not parse XML document.");
	}
}

